;(() => {
    const Util = function () {}

    const loadGeoJSONByModule = module => {
        return new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest()
            const requestUrl = `./data/${module}.geojson`
            xhr.open('get', requestUrl)
            xhr.send()
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    // 200
                    if (xhr.status == 200 && xhr.responseText) {
                        resolve(JSON.parse(xhr.responseText))
                    } else {
                        reject(`状态码为：${xhr.status}`)
                    }
                }
            }
            xhr.onerror = function () {
                console.log('error')
                reject([])
            }
            xhr.onabort = function () {
                console.log('onabort')
                reject([])
            }
        })
    }

    Util.prototype.loadGeoJSONByModule = loadGeoJSONByModule

    // 工具函数,用于加载geojson
    Util.prototype.loadAllGeoJSON = modules => {
        return new Promise((resolve, reject) => {
            const promiseList = modules.map(module => loadGeoJSONByModule(module))
            Promise.all(promiseList).then(res => {
                resolve(res)
            })
        })
    }

    // 返回自定义popup-dom
    Util.prototype.customPopup = properties => {
        // 省市名称
        const areaName = properties.name
        // 类型
        let type = '无'
        switch (properties.level) {
            case 'province': {
                type = '省'
                break
            }
            case 'city': {
                type = '市'
                break
            }
            case 'district': {
                type = '区'
                break
            }
            default: {
                type = properties.level
            }
        }
        const container = document.createElement('div')
        container.className = 'rambler-popup-container'
        container.innerHTML = `
            <a class="title" href="https://www.baidu.com/s?wd=${areaName}" target="_blank">${areaName}</a>
            <p class='rambler-text'>地区类型：${type}</p>
            <p class='rambler-text'>地区编码：${properties.adcode}</p>
            <p class='rambler-text'>子节点个数：${properties.childrenNum}</p>
        `
        return container
    }
    Util.prototype.formatTraceData = function (raw) {
        if (!Array.isArray(raw)) {
            return []
        }
        const arr = []
        for (let line of raw) {
            let temp = []
            for (let coor of line) {
                temp.push([coor[1], coor[0]])
            }
            arr.push(temp)
        }
        return arr
    }
    const util = new Util()
    window.Util = util
})()
